<?php
/**
 * Description: 获取错题列表
 * User: devforma
 * Date: 15/1/7
 * Time: 15:49
 */

class ErrorQuestionList implements iController {

    private $userIdField, $userId;
    
    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $uddb;
        
        $data = array();
        $u = validateUser($queryArgs);
        if ($u==0 || empty($queryArgs['order']) || !in_array($queryArgs['order'], array('asc', 'desc'))
            || !isset($queryArgs['offset']) || !is_numeric($queryArgs['offset']) || $queryArgs['offset']<0
            || empty($queryArgs['count']) || !is_numeric($queryArgs['count']) || $queryArgs['count']<1) {
            return genOutput(1000);
        }

        if ($u == 1) {
            $this->userIdField = 'user_id';
            $this->userId = $queryArgs['user_id'];
        } elseif ($u == 2) {
            $this->userIdField = 'guest_id';
            $this->userId = $queryArgs['guest_id'];
        } else {
            return genOutput(1000);
        }

        //获取日期
        $sql = "SELECT DISTINCT(`test_time`) FROM `{$uddb->userTests}`
                WHERE `{$this->userIdField}`=:user AND `is_wrong`=1
                ORDER BY `test_time` {$queryArgs['order']}";
        $dateArray = $uddb->getCol($sql, array(':user' => $this->userId));
        if (!empty($dateArray)) {
            foreach ($dateArray as &$date) {
                $date = date('Y-m-d', strtotime($date));
            }

            //日期处理
            $dateArray = array_values(array_unique($dateArray));
	if (!isset($dateArray[$queryArgs['offset']])) return genOutput(1000);
 
            $endIndex = $queryArgs['offset'] + $queryArgs['count'];
            if ($endIndex > count($dateArray))
                $endIndex = count($dateArray);

            if ($queryArgs['order'] == 'asc') {
                $startTime = strtotime($dateArray[$queryArgs['offset']]);
                $endTime = strtotime($dateArray[$endIndex-1]) + 24*60*60;
            } else {
                $startTime = strtotime($dateArray[$endIndex-1]);
                $endTime = strtotime($dateArray[$queryArgs['offset']]) + 24*60*60;
            }

            if ($startTime == $endTime) { //只有一天，则endTime为startTime的一天之后
               $endTime = $startTime + 24*60*60;
            }
            
            $sql = "SELECT * FROM `{$uddb->userTests}` WHERE `{$this->userIdField}`=:user
                    AND UNIX_TIMESTAMP(`test_time`)>={$startTime}
                    AND UNIX_TIMESTAMP(`test_time`)<{$endTime} AND `is_wrong`=1
                    ORDER BY `test_time` {$queryArgs['order']}";
            $questions = $uddb->getResults($sql, array(':user'=>$this->userId));

            $list = array();
            foreach ($questions as $q) {
                $date = date('Y-m-d', strtotime($q->test_time)); //转换为日期
                if (!isset($list["$date"])) { //list中不存在该日期对应的题目信息，则初始化
                    $list["$date"] = array();
                }

                $list["$date"][] = $q->question_id; //相同日期的题目id存储到该日期对应的的数组
            }

            //格式化
            foreach ($list as $date => $cell) {
                $filteredCell = array_unique($cell); //过滤相同id的题
                $data[] = array(
                    'date' => $date,
                    'count' => count($filteredCell),
                    'ids' => implode(',', $filteredCell)
                );
            }
        }

        return genOutput(1, array('questions' => $data));
    }
}
